package com.hcss.common.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.bind.annotation.PathVariable;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @function : springmvc下的aop处理工具类
 */
public class AOPUtil {
	
	/**
	 * 获取当前请求的方法
	 * @param point
	 * @return
	 */
	public static Method getRequestMethod(ProceedingJoinPoint point){
		MethodSignature signature = (MethodSignature) point.getSignature();
		return signature.getMethod();
	}

    /**
     * 获取当前请求参数map集合
     * @param point
     * @return
     */
	public static Map<String, Object> getRequestAttrubiteMap(ProceedingJoinPoint point){
        Method method = getRequestMethod(point);
        Annotation[][] annotationses = method.getParameterAnnotations();
        Object[] args = point.getArgs();
        Map<String, Object> result = new HashMap<>();
        for (int i = 0; i < annotationses.length; i++) {
            Annotation[] annotations = annotationses[i];
            if (annotations.length == 0) {
                continue;
            }
            for (Annotation annotation : annotations) {
                if (!(annotation instanceof PathVariable)) {
                    continue;
                }

                String name = ((PathVariable) annotation).value();
                result.put(name, args[i]);
            }
        }

        return result;
    }
}
